// ActionScript file
package com.app.business{
	import com.adobe.cairngorm.business.ServiceLocator;
	import com.app.factories.MahasiswaFactory;
	import com.app.model.ModelLocator;
	import com.app.vo.MahasiswaVO;
	import com.app.vo.VO;
	
	import mx.controls.Alert;
	import mx.rpc.AsyncToken;
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	

    public class MahasiswaDelegate implements IResponder{
        protected var responder:IResponder;
        protected var service:HTTPService;
        protected var mahasiswaVo:MahasiswaVO;
        protected var loginVo:VO;
        protected var Com:String;
        protected var Act:String;
        protected var Param:String;
        protected var rs:Array;
        
        public function MahasiswaDelegate (responder : IResponder){
            this.service = ServiceLocator.getInstance().getHTTPService( "services" );
            this.responder = responder;
            this.rs = new Array();
        }
        
        public function getDokTaMahasiswa(mahasiswaVo:MahasiswaVO):void{
            Param = mahasiswaVo.mhsId;
            Act = "getDokTaMahasiswa";
            Com = "mahasiswa";
            var token:AsyncToken = service.send({com:Com, act:Act,params:Param});
            token.addResponder(this);
            this.mahasiswaVo = mahasiswaVo;
        } 
        
        public function getInfoMahasiswa(mahasiswaVo:MahasiswaVO):void{
            if(mahasiswaVo.mhsId!=null)
                Param = mahasiswaVo.mhsId;
            else 
                Param = mahasiswaVo.mhsUsername;

            Act = "getInfoMahasiswa";
            Com = "mahasiswa";
            var token:AsyncToken=service.send({com:Com, act:Act,params:Param});
            token.addResponder(this);
            this.mahasiswaVo = mahasiswaVo;
            
        }
        
        public function getTugasAkhirMahasiswa(mahasiswaVo:MahasiswaVO):void{
            Param = mahasiswaVo.mhsId;
            Act = "getTugasAkhirMahasiswa";
            Com = "mahasiswa";
            var token:AsyncToken=service.send({com:Com, act:Act,params:Param});
            token.addResponder(this);
            this.mahasiswaVo = mahasiswaVo;
            
        }
        

       public function getListDosen(mahasiswaVo:MahasiswaVO):void{
            this.Param = mahasiswaVo.mhsId;
            Act = 'getListDosen';
            Com = 'mahasiswa';
            var token:AsyncToken=service.send({com:Com, act:Act,params:Param});
            token.addResponder(this);
            this.mahasiswaVo=mahasiswaVo;
        }
        
        public function doUpdateProfilMahasiswa(mahasiswaVo:MahasiswaVO):void{
        	this.Param = mahasiswaVo.mhsNama + '|' + mahasiswaVo.mhsAlamat + '|' + mahasiswaVo.mhsNoTelp + '|' + mahasiswaVo.mhsEmail + '|' + mahasiswaVo.mhsId;
       		Act = 'doUpdateProfilMahasiswa';
       		Com = "mahasiswa";
       		//Alert.show(Param);
       		var token:AsyncToken = service.send({com:Com, act:Act,params:Param});
       		token.addResponder(this);
       		this.mahasiswaVo = mahasiswaVo;
        }       
        
        public function result(dt:Object):void{
            var event:ResultEvent= ResultEvent(dt);

            if(event.result!='false'){
            	if(Act=='getDokTaMahasiswa'){
            	    this.mahasiswaVo=MahasiswaFactory.buildDokTaMahasiswa(event.result as String,this.mahasiswaVo);
                    responder.result(this.mahasiswaVo);
                }else if(Act=='getInfoMahasiswa'){
                    this.mahasiswaVo=MahasiswaFactory.buildInfoMahasiswa(event.result as String,this.mahasiswaVo);
                    responder.result(this.mahasiswaVo);
                }else if(Act=='getTugasAkhirMahasiswa'){
                    this.mahasiswaVo=MahasiswaFactory.buildTugasAkhirMahasiswa(event.result as String,this.mahasiswaVo);
                    responder.result(this.mahasiswaVo);
                }else if(Act=='getListDosen'){
                //	Alert.show(event.result as String);
                    this.mahasiswaVo=MahasiswaFactory.buildListDosen(event.result as String,this.mahasiswaVo);
                    responder.result(this.mahasiswaVo);   
                }else if(this.Act=='doUpdateProfilMahasiswa'){
                	//Alert.show(event.result as String); //uncommand this line to debug result of services           
	               responder.result(this.mahasiswaVo);
	            }
            }else{
                responder.fault(null);
            } 
        }
        
        public function fault(info:Object):void{
            var err:FaultEvent=FaultEvent(info);
            ModelLocator.getInstance().mahasiswa.statusMessage='Get Mahasiswa Failed';
        }
    }
}